<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cache Handler Function</title>
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Smarty3 Manual">
<link rel="up" href="advanced.features.html" title="Chapter 15. Advanced Features">
<link rel="prev" href="advanced.features.outputfilters.html" title="Output Filters">
<link rel="next" href="template.resources.html" title="Resources">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="navheader">
<table width="100%" summary="Navigation header">
<tr><th colspan="3" align="center">Cache Handler Function</th></tr>
<tr>
<td width="20%" align="left">
<a accesskey="p" href="advanced.features.outputfilters.html">Prev</a> </td>
<th width="60%" align="center">Chapter 15. Advanced Features</th>
<td width="20%" align="right"> <a accesskey="n" href="template.resources.html">Next</a>
</td>
</tr>
</table>
<hr>
</div>
<div class="sect1" title="Cache Handler Function">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="section.template.cache.handler.func"></a>Cache Handler Function</h2></div></div></div>
<p>
    As an alternative to using the default file-based caching mechanism, you
    can specify a custom cache handling function that will be used to
    <code class="literal">read</code>, <code class="literal">write</code> and
    <code class="literal">clear</code> cached files.
   </p>
<p>
    Create a function in your application that Smarty will use as a
    cache handler. Set the name of it in the
    <a class="link" href="variable.cache.handler.func.html" title="$cache_handler_func">
    <em class="parameter"><code>$cache_handler_func</code></em></a>
    class variable. Smarty will now use this to handle cached data.
    </p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>
    The first argument is the action, which will be one of
    <code class="literal">read</code>, <code class="literal">write</code> and
    <code class="literal">clear</code>.
    </p></li>
<li class="listitem"><p>
    The second parameter is the Smarty object.
    </p></li>
<li class="listitem"><p>The third parameter
    is the cached content. Upon a <code class="literal">write</code>, Smarty passes the
    cached content  in these parameters. Upon a <code class="literal">read</code>,
    Smarty expects your function to accept this parameter by reference and
    populate it with the cached data. Upon a <code class="literal">clear</code>, pass a
    dummy variable here since it is not used.
    </p></li>
<li class="listitem"><p>
    The fourth parameter is the <em class="parameter"><code>name</code></em> of the template
    file, needed for read/write.
    </p></li>
<li class="listitem"><p>
    The fifth parameter is the optional <em class="parameter"><code>$cache_id</code></em>.
    </p></li>
<li class="listitem"><p>
    The sixth is the optional <a class="link" href="variable.compile.id.html" title="$compile_id">
    <em class="parameter"><code>$compile_id</code></em></a>.
    </p></li>
<li class="listitem"><p>
    The seventh and last parameter <em class="parameter"><code>$exp_time</code></em>
    was added in Smarty-2.6.0.
   </p></li>
</ul></div>
<div class="example">
<a name="id663542"></a><p class="title"><b>Example 15.16. Example using MySQL as a cache source</b></p>
<div class="example-contents"><pre class="programlisting">

&lt;?php
/**************************************************
example usage:

include('Smarty.class.php');
include('mysql_cache_handler.php');

$smarty = new Smarty;
$smarty-&gt;cache_handler_func = 'mysql_cache_handler';

$smarty-&gt;display('index.tpl');


mysql database is expected in this format:

create database SMARTY_CACHE;

create table CACHE_PAGES(
CacheID char(32) PRIMARY KEY,
CacheContents MEDIUMTEXT NOT NULL
);

**************************************************/

function mysql_cache_handler($action, $smarty_obj, &amp;$cache_content, $tpl_file=null, $cache_id=null, $compile_id=null, $exp_time=null)
{
	// set db host, user and pass here
	$db_host = 'localhost';
	$db_user = 'myuser';
	$db_pass = 'mypass';
	$db_name = 'SMARTY_CACHE';
	$use_gzip = false;

	// create unique cache id
	$CacheID = md5($tpl_file.$cache_id.$compile_id);

	if(! $link = mysql_pconnect($db_host, $db_user, $db_pass)) {
		trigger_error('cache_handler: could not connect to database');
		return false;
	}
	mysql_select_db($db_name);

	switch ($action) {
		case 'read':
			// read cache from database
			$results = mysql_query("select CacheContents from CACHE_PAGES where CacheID='$CacheID'");
			if(!$results) {
				trigger_error('cache_handler: query failed.');
			}
			$row = mysql_fetch_array($results,MYSQL_ASSOC);

			if($use_gzip &amp;&amp; function_exists('gzuncompress')) {
				$cache_content = gzuncompress($row['CacheContents']);
			} else {
				$cache_content = $row['CacheContents'];
			}
			$return = $results;
			break;
		case 'write':
			// save cache to database

			if($use_gzip &amp;&amp; function_exists("gzcompress")) {
				// compress the contents for storage efficiency
				$contents = gzcompress($cache_content);
			} else {
				$contents = $cache_content;
			}
			$results = mysql_query("replace into CACHE_PAGES values(
							'$CacheID',
							'".addslashes($contents)."')
						");
			if(!$results) {
				trigger_error('cache_handler: query failed.');
			}
			$return = $results;
			break;
		case 'clear':
			// clear cache info
			if(empty($cache_id) &amp;&amp; empty($compile_id) &amp;&amp; empty($tpl_file)) {
				// clear them all
				$results = mysql_query('delete from CACHE_PAGES');
			} else {
				$results = mysql_query("delete from CACHE_PAGES where CacheID='$CacheID'");
			}
			if(!$results) {
				trigger_error('cache_handler: query failed.');
			}
			$return = $results;
			break;
		default:
			// error, unknown action
			trigger_error("cache_handler: unknown action \"$action\"");
			$return = false;
			break;
	}
	mysql_close($link);
	return $return;

}

?&gt;

</pre></div>
</div>
<br class="example-break">
</div>
<div class="navfooter">
<hr>
<table width="100%" summary="Navigation footer">
<tr>
<td width="40%" align="left">
<a accesskey="p" href="advanced.features.outputfilters.html">Prev</a> </td>
<td width="20%" align="center"><a accesskey="u" href="advanced.features.html">Up</a></td>
<td width="40%" align="right"> <a accesskey="n" href="template.resources.html">Next</a>
</td>
</tr>
<tr>
<td width="40%" align="left" valign="top">Output Filters </td>
<td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
<td width="40%" align="right" valign="top"> Resources</td>
</tr>
</table>
</div>
</body>
</html>
